package com.pengheng.example2;

import org.bouncycastle.asn1.*;

import java.io.IOException;

public class Person extends ASN1Object implements ASN1Encodable {
    private DERPrintableString name;
    private ASN1Integer age;
    private DERIA5String email;
    private Address address;

    public Person() {
        super();
    }

    public Person(DERPrintableString name, ASN1Integer age, DERIA5String email, Address address) {
        super();
        this.name = name;
        this.age = age;
        this.email = email;
        this.address = address;
    }

    public DERPrintableString getName() {
        return name;
    }

    public void setName(DERPrintableString name) {
        this.name = name;
    }

    public ASN1Integer getAge() {
        return age;
    }

    public void setAge(ASN1Integer age) {
        this.age = age;
    }

    public DERIA5String getEmail() {
        return email;
    }

    public void setEmail(DERIA5String email) {
        this.email = email;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public ASN1Primitive toASN1Primitive() {
        return new DERSequence(new ASN1Encodable[]{name, age, email, address});
    }

    public static Person getInstance(Object obj) throws IOException {
        if (obj instanceof Person) {
            return (Person) obj;
        } else if (obj != null) {
            return new Person(DERPrintableString.getInstance(((DERSequence) obj).getObjectAt(0)),
                    ASN1Integer.getInstance(((DERSequence) obj).getObjectAt(1)),
                    DERIA5String.getInstance(((DERSequence) obj).getObjectAt(2)),
                    Address.getInstance(((DERSequence) obj).getObjectAt(3)));
        }
        throw new IllegalArgumentException("Invalid Person: " + obj.getClass().getName());
    }
}